package cn.tianya.irock.util;

import java.util.ArrayList;
import java.util.Properties;

public class ExtendedAttributes {
	
	private Properties props;
	
	public ExtendedAttributes() {
		props = new Properties();
	}
	
	public String getExtendedAttribute(String name) {
		String value = props.getProperty(name);
		
		if (value == null) {
			value = "";
		}
		return value;
	}
	
	public void setExtendedAttribute(String name, String value) {
		if (name.length() == 0 || value == null) {
			props.remove(name);
		} else {
			props.put(name, value);
		}
	}
	
	public int getExtendedAttributesCount() {
		return props.size();
	}
	
	public String toString() {
		ArrayList<Class> alias = new ArrayList<Class>();
		
		alias.add(props.getClass());
		return JSONUtil.toJSONString(props, alias);
	}
	
	public void setSerializerAttributes(Properties props) {
		this.props = props;
	}

	protected boolean getBool(String name, boolean defaultValue) {
		String value = getExtendedAttribute(name);
		
		if (value.length() == 0) {
			return defaultValue;
		}
		
		return Boolean.parseBoolean(value);
	}
	
	protected int getInt(String name, int defaultValue) {
		String value = getExtendedAttribute(name);
		
		if (value.length() == 0) {
			return defaultValue;
		}
		
		return Integer.parseInt(value);
	}
	
	protected String getString(String name, String defaultValue) {
		String value = getExtendedAttribute(name);
		
		if (value.length() == 0) {
			return defaultValue;
		}
		return value;
	}
}
